package com.infoDiffusion.service;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.infoDiffusion.bean.EmergencyBean;
import com.infoDiffusion.bean.KnowledgeBean;
import com.infoDiffusion.bean.MaterialBean;
import com.infoDiffusion.bean.UserBean;
import com.infoDiffusion.dao.ReportersDao;


@Service
@Transactional(propagation = Propagation.REQUIRED)
public class ReportersService {
	@Autowired
	private ReportersDao rDao;

	public boolean addk(KnowledgeBean kbean) {
		return rDao.add(kbean);
	}
	public boolean addm(MaterialBean mbean) {
		return rDao.add(mbean);
	}
	public boolean adde(EmergencyBean ebean) {
		return rDao.add(ebean);
	}
	
	public List listk(KnowledgeBean kbean) {
		String hql = "from KnowledgeBean";
		return rDao.getList(hql);
	}
	public List listm(MaterialBean mbean) {
		String hql = "from MaterialBean";
		return rDao.getList(hql);
	}
	public List listmcount(MaterialBean mbean) {
		String hql = "select new MaterialBean(m.type,sum(m.num),m.unit) from MaterialBean m  group by m.type";
		List list = rDao.getListTest(hql);
		return list;
	}
	
	public List<MaterialBean> getList(MaterialBean mbean){
		String sql = "select type,sum(num),unit from material group by type";
		List<Map<String, Object>> list = rDao.getSQLList(sql,null);
		System.out.println(list);
		List<MaterialBean> li = new ArrayList<MaterialBean>();
		String type = null;
		String unit = null;
		Integer num = null;
		MaterialBean m = null;
		for (Map<String, Object> map : list) {
//			mbean.setName(map.get("months")+"");
			System.out.println(map.get("sum(num)"));
			type = map.get("type").toString();
			num = Integer.parseInt(map.get("sum(num)").toString());
			unit = map.get("unit").toString();
//			m = new MaterialBean(type, num, unit);
			li.add(m);
		}
		return li;
	}

	
	public List liste(EmergencyBean ebean) {
		String hql = "from EmergencyBean";
		return rDao.getList(hql);
	}
	
	public boolean updateknowledge(KnowledgeBean kbean) {
		return rDao.update(kbean);
	}
	public boolean updatem(MaterialBean mbean) {
		return rDao.update(mbean);
	}
	public boolean updatee(EmergencyBean ebean) {
		return rDao.update(ebean);
	}
	public KnowledgeBean getById(int id) {
		return (KnowledgeBean) rDao.getById(KnowledgeBean.class, id);
	}
	
	public MaterialBean getByIdm(int id) {
		return (MaterialBean) rDao.getById(MaterialBean.class, id);
	}
	public EmergencyBean getByIde(int id) {
		return (EmergencyBean) rDao.getById(EmergencyBean.class, id);
	}
	public boolean delete(KnowledgeBean kbean) {
		rDao.delete(kbean);
		return true;
	}
	public boolean delete(MaterialBean mbean) {
		rDao.delete(mbean);
		return true;
	}
	public boolean delete(EmergencyBean ebean) {
		rDao.delete(ebean);
		return true;
	}
	public boolean updateById(int id){
		String hql = "update K_KnowledgeBean set username=?,password=?,power=?,create_date=?";
		return rDao.updateById(hql, id);
	}
	
}
